# Maintainer: J. Peter Mugaas <jpmugaas@suddenlink.net>
# Contributor: wirx6 <wirx654@gmail.com>

_realname=Vulkan-ValidationLayers
pkgbase=mingw-w64-vulkan-validation-layers
pkgname=("${MINGW_PACKAGE_PREFIX}-vulkan-validation-layers")
pkgver=1.2.162
pkgrel=3
pkgdesc='Vulkan Validation Layers (mingw-w64)'
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64')
url="https://www.khronos.org/vulkan/"
groups=("${MINGW_PACKAGE_PREFIX}-vulkan-devel")
license=('Apache')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-vulkan-loader")
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-glslang"
             "${MINGW_PACKAGE_PREFIX}-python"
             "${MINGW_PACKAGE_PREFIX}-vulkan-headers"
             "${MINGW_PACKAGE_PREFIX}-spirv-headers"
             "${MINGW_PACKAGE_PREFIX}-spirv-tools")
source=("${_realname}-${pkgver}.tar.gz"::"https://github.com/KhronosGroup/${_realname}/archive/v${pkgver}.tar.gz"
        001-fix-json-layers-install-dest.patch
        002-proper-def-files-for-32bit.patch
        003-install-layout.patch)
sha256sums=('83619b81a86e733383a71ea54f0f5e64e194bf596ee8ec4e36217bb0b29b8917'
            '38b2b5d6e6fc45c3761dde9fe5125356ac76b471bfacd9177aa65c8422dcbd17'
            '7b9f0ee2477119de1f186d5639731b03f54199f1eceedf0204d51e736f8e6585'
            '6f8a3cdf845763c12515f18b26fad08772a20f0a4b380a667ef4f12a218ba810')

prepare() {
  cd ${srcdir}/${_realname}-${pkgver}

  patch -p1 -i ${srcdir}/001-fix-json-layers-install-dest.patch
  patch -p1 -i ${srcdir}/002-proper-def-files-for-32bit.patch
  patch -p1 -i ${srcdir}/003-install-layout.patch
}

build() {
  [[ -d ${srcdir}/build-${CARCH} ]] && rm -rf ${srcdir}/build-${CARCH}
  mkdir -p ${srcdir}/build-${CARCH} && cd ${srcdir}/build-${CARCH}

  declare -a extra_config
  if check_option "debug" "n"; then
    extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  CXXFLAGS+=" -Wno-unused-function" \
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -G "Ninja" \
    "${extra_config[@]}" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DGLSLANG_INSTALL_DIR=${MINGW_PREFIX} \
    -DSPIRV_HEADERS_INSTALL_DIR=${MINGW_PREFIX} \
    -DBUILD_LAYER_SUPPORT_FILES=ON \
    -DBUILD_TESTS=OFF \
    ../${_realname}-${pkgver}

  ${MINGW_PREFIX}/bin/cmake.exe --build ./
}

package() {
  cd ${srcdir}/build-${CARCH}
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake.exe --build ./ --target install

  install -dm755 ${pkgdir}${MINGW_PREFIX}/share/doc/vulkan-validation-layers/
  cp -r ${srcdir}/${_realname}-${pkgver}/docs/* ${pkgdir}${MINGW_PREFIX}/share/doc/vulkan-validation-layers/
  install -Dm644 ${srcdir}/${_realname}-${pkgver}/LICENSE.TXT ${pkgdir}${MINGW_PREFIX}/share/licenses/vulkan-validation-layers/LICENSE
}
